import {
  CategoryActionTypes,
  GET_CATEGORY_LIST,
  GET_CATEGORY_LIST_FAILURE,
  GET_CATEGORY_LIST_SUCCESS,
} from '../actions/category'
import { Category } from '../models/category'

export interface CategoryState {
  loaded: boolean
  success: boolean
  categoryList: Category[]
}

const initialState: CategoryState = {
  loaded: false,
  success: false,
  categoryList: [],
}

export default function categoryReducer(
  state = initialState,
  action: CategoryActionTypes
): CategoryState {
  switch (action.type) {
    case GET_CATEGORY_LIST:
      return initialState
    case GET_CATEGORY_LIST_SUCCESS:
      return {
        loaded: true,
        success: true,
        categoryList: action.payload,
      }
    case GET_CATEGORY_LIST_FAILURE:
      return initialState
    default:
      return state
  }
}
